package me.luger.project.picc.controller.admin;

import me.luger.core.commom.util.DateUtil;
import me.luger.core.commom.util.StringUtil;
import me.luger.core.dao.nutz.entity.page.DataTableRequest;
import me.luger.core.dao.nutz.entity.page.DataTableResult;
import me.luger.core.mvc.controller.BaseController;
import me.luger.project.picc.entity.Insurance;
import me.luger.project.picc.entity.PrizeList;
import me.luger.project.picc.service.InsuranceService;
import me.luger.project.picc.service.PrizeListService;
import me.luger.project.picc.service.picc.TongjiService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * Created by luger on 2017/5/11.
 */
@Controller
@RequestMapping("/private/picc/tongji")
public class TongjiController extends BaseController{
    private String viewBasePath = "/admin/modules/tongji/";

    @Autowired
    private TongjiService tongjiService;

    @Autowired
    private PrizeListService prizeListService;

    @Autowired
    private InsuranceService insuranceService;


    @RequestMapping(value = "",name = "统计页面")
    public ModelAndView index(@RequestParam(defaultValue = "0") int typeId){
        Long allUserCount = 0L,todayUserCount=0L,allTimesCount=0L,todayTimesCount=0L,allPrizedCount=0L,todayPrizedCount=0L;

        if(typeId == 0){
            allUserCount = tongjiService.getAllUserCount();
            todayUserCount = tongjiService.getUserCountByDate(DateUtil.nowAsDate());

            allTimesCount = tongjiService.getAllTimesCount();
            todayTimesCount = tongjiService.getTimesCountByDate(DateUtil.nowAsDate());

            allPrizedCount = prizeListService.getAllPrizedUserCount(0);
            todayPrizedCount = prizeListService.getPrizedUserCountByDate(0,DateUtil.nowAsDate());
        }else{
            allUserCount = tongjiService.getActivityAllUserCount(typeId);
            todayUserCount = tongjiService.getActivityUserCountByDate(typeId,DateUtil.nowAsDate());

            allTimesCount = tongjiService.getActivityAllTimesCount(typeId);
            todayTimesCount = tongjiService.getActivityTimesCountByDate(typeId,DateUtil.nowAsDate());

            allPrizedCount = prizeListService.getAllPrizedUserCount(typeId);
            todayPrizedCount = prizeListService.getPrizedUserCountByDate(typeId,DateUtil.nowAsDate());
        }


        setAttr("allUserCount",allUserCount);
        setAttr("todayUserCount",todayUserCount);
        setAttr("allTimesCount",allTimesCount);
        setAttr("todayTimesCount",todayTimesCount);
        setAttr("allPrizedCount",allPrizedCount);
        setAttr("todayPrizedCount",todayPrizedCount);
        setAttr("typeId",typeId);

        return btlView(viewBasePath + "index");
    }
    @RequestMapping(value = "/otherTongji",name = "其他统计页面")
    public ModelAndView otherTongji(){

        for(int typeId=5;typeId<=14;typeId++){
            setAttr("huodong"+typeId+"AllUserCount",tongjiService.getActivityAllUserCount(typeId));
            setAttr("huodong"+typeId+"TodayUserCount",tongjiService.getActivityUserCountByDate(typeId,DateUtil.nowAsDate()));
            setAttr("huodong"+typeId+"AllTimesCount",tongjiService.getActivityAllTimesCount(typeId));
            setAttr("huodong"+typeId+"TodayTimesCount",tongjiService.getActivityTimesCountByDate(typeId,DateUtil.nowAsDate()));
        }


        return btlView(viewBasePath + "otherTongji");
    }

    @RequestMapping(value = "/allPrizedData",name = "获取用户中奖列表列表")
    @ResponseBody
    public Object allPrizedData(@RequestBody final DataTableRequest dataTableRequest){
        String typeIdStr = dataTableRequest.getExtend().get("typeId");
        int typeId = 0;
        if(StringUtil.isNotBlank(typeIdStr)){
            typeId = Integer.parseInt(typeIdStr);
        }

        DataTableResult<PrizeList> prizeListDataTableResult = prizeListService.getAllPrizedData(dataTableRequest,typeId);

        List<PrizeList> prizeLists = prizeListDataTableResult.getData();

        for(PrizeList prizeList : prizeLists){
            if(StringUtil.isBlank(prizeList.getUserName()) && prizeList.getTypeId() == 3){
                Insurance insurance = insuranceService.fetchById(prizeList.getBizId());
                prizeList.setUserName(insurance.getUserName());
            }
        }
        return prizeListDataTableResult;
    }
}
